import * as turf from '@turf/turf';

import * as mars3d from 'mars3d';

// export declare interface instanceObject {
//     [key: string]: string;
// }

/**
 * JSON转url参数
 * @param data Json格式数据
 * */
export const formatJsonToUrlParams = (data) => {
    return typeof data === 'object'
        ? Object.keys(data)
              .map((key) => {
                  return `${encodeURIComponent(key)}=${encodeURIComponent(
                      data[key],
                  )}`;
              })
              .join('&')
        : '';
};

/**
 * 千分位
 * @param data 原始数值
 * */
export const toThousands = (data) => {
    return (data || 0).toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,');
};

/**
 * 数字转成汉字
 * @params num === 要转换的数字
 * @return 汉字
 * */
export const toChinesNum = (num) => {
    const changeNum = [
        '零',
        '一',
        '二',
        '三',
        '四',
        '五',
        '六',
        '七',
        '八',
        '九',
    ];
    const unit = ['', '十', '百', '千', '万'];
    // eslint-disable-next-line no-param-reassign
    num = parseInt(num, 10);
    const getWan = (temp) => {
        const strArr = temp.toString().split('').reverse();
        let newNum = '';
        const newArr = [];
        strArr.forEach((item, index) => {
            newArr.unshift(
                item === '0' ? changeNum[item] : changeNum[item] + unit[index],
            );
        });
        const numArr = [];
        newArr.forEach((m, n) => {
            if (m !== '零') numArr.push(n);
        });
        if (newArr.length > 1) {
            newArr.forEach((m, n) => {
                if (newArr[newArr.length - 1] === '零') {
                    if (n <= numArr[numArr.length - 1]) {
                        newNum += m;
                    }
                } else {
                    newNum += m;
                }
            });
        } else {
            newNum = newArr[0];
        }

        return newNum;
    };
    const overWan = Math.floor(num / 10000);
    let noWan = num % 10000;
    if (noWan.toString().length < 4) {
        noWan = `0${noWan}`;
    }
    return overWan ? `${getWan(overWan)}万${getWan(noWan)}` : getWan(num);
};

export const getImgeUrl = (name) => {
    // return  require(`../assets/images/mars3dimages/${name}`)
    return  require(`./../assets/images/mars3dimages/${name}`)
};

export const useDefaultImage = (event) => {
    const img = event.srcElement;
    img.src = getImgeUrl('p404.svg');
    img.onerror = null; // 防止闪图
};

export function computCenterPoint(positions) {
    const polygon = turf.polygon([[...positions, positions[0]]]);

    const centroid = turf.centroid(polygon);

    return [...centroid.geometry.coordinates, 0];
}

export function computCamera(options) {
    const { point, camera, alt, distance, offsetangle } = options;

    const point1 = turf.point([point.lng, point.lat]);

    const point2 = turf.point([camera.lng, camera.lat]);

    const bearing = turf.bearing(point1, point2); // 获取目标点-相机点与正北方向轴的夹角 [-180,180]

    // eslint-disable-next-line @typescript-eslint/no-loss-of-precision
    const destination = turf.destination(
        point1,
        distance || 0.163021967451384,
        bearing,
    );

    const newcamera = destination.geometry.coordinates;

    return {
        lat: newcamera[1],
        lng: newcamera[0],
        alt: alt || 300,
        heading: offsetangle ? 180 + bearing + offsetangle : 180 + bearing + 15,
        pitch: -38,
    };
}

export function updateAutoSurfaceHeight(
    map,
    position,
) {
    if (!position) {
        return;
    }

    const cartesian3 = mars3d.Cesium.Cartesian3.fromDegrees(
        position[0],
        position[1],
        0,
    );

    // 点贴模型测试

    // eslint-disable-next-line consistent-return
    return mars3d.PointUtil.getSurfaceHeight(map.scene, cartesian3, {
        has3dtiles: true,
    });
}

export default formatJsonToUrlParams;

// 获取assets静态资源
